package test.mywaterfall;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class WaterFall extends ScrollView implements OnClickListener{

	private LinearLayout mainLayout, waterfallLayout, leftLayout, middleLayout, rightLayout;
	private View footer;
	private OnScrollListener onScrollListener;

	public WaterFall(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public WaterFall(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public WaterFall(Context context) {
		super(context);
		init();
	}

	private void init() {
		mainLayout = new LinearLayout(getContext());
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		addView(mainLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		waterfallLayout = new LinearLayout(getContext());
		waterfallLayout.setOrientation(LinearLayout.HORIZONTAL);
		mainLayout.addView(waterfallLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		leftLayout = new LinearLayout(getContext());
		leftLayout.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		lp.weight = 1;
		waterfallLayout.addView(leftLayout, lp);

		middleLayout = new LinearLayout(getContext());
		middleLayout.setOrientation(LinearLayout.VERTICAL);
		lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		lp.weight = 1;
		lp.setMargins(2, 0, 2, 0);
		waterfallLayout.addView(middleLayout, lp);

		rightLayout = new LinearLayout(getContext());
		rightLayout.setOrientation(LinearLayout.VERTICAL);
		lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		lp.weight = 1;
		waterfallLayout.addView(rightLayout, lp);
	}
	
	private ImageView generateImageView(Bitmap bmp) {
		ImageView image = new ImageView(getContext());
		image.setAdjustViewBounds(true);
		image.setScaleType(ScaleType.CENTER_CROP);
		image.setImageBitmap(bmp);
		image.setOnClickListener(this);
		return image;
	}

	public void addToLeft(Bitmap bmp) {
		leftLayout.addView(generateImageView(bmp), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	public void addToMiddle(Bitmap bmp) {
		middleLayout.addView(generateImageView(bmp), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}

	public void addToRight(Bitmap bmp) {
		rightLayout.addView(generateImageView(bmp), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}
	
	public void addView(Bitmap bmp) {
		switch(getMaxChildCount() % 3) {
		case 2:
			addToRight(bmp);
			break;
		case 1:
			addToMiddle(bmp);
			break;
		case 0:
			addToLeft(bmp);
			break;
		}
	}

	public void addFooter(View footer) {
		this.footer = footer;
		mainLayout.addView(footer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}
	
	@Override
	public void removeAllViews() {
		leftLayout.removeAllViews();
		middleLayout.removeAllViews();
		rightLayout.removeAllViews();
		removeFooter();
	}

	public void setFooterOnClickListener(OnClickListener listener) {
		if (footer != null) {
			footer.setOnClickListener(listener);
		}
	}

	public View removeFooter() {
		if (footer != null) {
			mainLayout.removeView(footer);
			footer = null;
		}
		return footer;
	}
	
	public boolean hasFooter() {
		return footer != null;
	}
	
	public void setOnScrollListener(OnScrollListener listener) {
		this.onScrollListener = listener;
	}
	
	@Override
	public void onClick(View v) {
		ViewGroup parent = (ViewGroup) v.getParent();
		int index = (parent.indexOfChild(v) + 1) * 3;
		if (parent == leftLayout) {
			index -= 2;
		} else if (parent == middleLayout) {
			index -= 1;
		}
		Toast.makeText(getContext(), String.format("position %d is clicked.", index), Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (onScrollListener != null) {
			int maxHeight = getMaxHeight();
			Rect visibleRect = new Rect();
			this.getLocalVisibleRect(visibleRect);
			if(visibleRect.top == 0 && t != 0) {
				onScrollListener.onTop(getMaxChildCount());
			} else if (visibleRect.bottom == maxHeight && t != maxHeight) {
				onScrollListener.onBottom(getMaxChildCount());
			}
		}
	}
	
	private int getMaxHeight() {
		int maxHeight = 0;
		maxHeight = leftLayout.getHeight() > middleLayout.getHeight() ? leftLayout.getHeight() : middleLayout.getHeight();
		maxHeight = maxHeight > rightLayout.getHeight() ? maxHeight : rightLayout.getHeight();
		return maxHeight;
	}
	
	
	public int getMaxChildCount() {
		return leftLayout.getChildCount() + middleLayout.getChildCount() + rightLayout.getChildCount();
	}


	public static interface OnScrollListener {
		public void onTop(int maxCount);
		public void onBottom(int maxCount);
	}
}
